package nuonuo.com.view.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import nuonuo.com.view.R;
import nuonuo.com.view.app.MyApplication;
import nuonuo.com.view.bean.User;
import nuonuo.com.view.customview.Logger;
import nuonuo.com.view.retrofit_interface.LoginService;
import nuonuo.com.view.utils.StringUtils;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;

public class RxJavaRetrofit extends AppCompatActivity {
    private EditText mEtUser, mEtPwd;
    private Button mBtnSubmit;
    private Retrofit mRetrofit;
    private Map<String, String> map;
    private LoginService mService;
    private TextView tvContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_java_retrofit);
        mEtPwd = (EditText) findViewById(R.id.et_pwd);
        mEtUser = (EditText) findViewById(R.id.et_user);
        mBtnSubmit = (Button) findViewById(R.id.btn_submit);
        mRetrofit = MyApplication.getRetrofit();
        tvContent = (TextView) findViewById(R.id.tv_content);

        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = mEtUser.getText().toString();
                String pwd = mEtPwd.getText().toString();

                if (StringUtils.isStringNull(account)) {
                    Toast.makeText(RxJavaRetrofit.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.isStringNull(pwd)) {
                    Toast.makeText(RxJavaRetrofit.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                mService = mRetrofit.create(LoginService.class);
                /**
                 * user +		手机,ID,第三方登录openid	微信对应code
                 pwd  +      MD5密码,第三方access_token
                 */
                map = new HashMap();
                map.put("user", account);
                map.put("pwd", StringUtils.parseStrToMd5L32(pwd));
                Logger.i("click-----------------");
//                Map<String, String> map = new HashMap<>();
//                map.put("auth", user.getR().getAuth());
//                Call<User> loginCall = mService.login("n08", map);
                Observable<User> observable = mService.loginIn("n08", map);
                observable
                        .flatMap(new Function<User, ObservableSource<ResponseBody>>() {
                            @Override
                            public ObservableSource<ResponseBody> apply(User user) throws Exception {
                                Map<String, String> map = new HashMap<>();
                                map.put("auth", user.getR().getAuth());
                                return mService.getData("j11",map);
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<ResponseBody>() {
                            @Override
                            public void accept(ResponseBody responseBody) throws Exception {
                                Logger.i(responseBody.string()+"----------------");
                            }
                        });
            }
        });
    }
}
